//
//  SouSuoViewController.m
//  livingDesign
//
//  Created by dlios on 15-1-30.
//  Copyright (c) 2015年 怡居. All rights reserved.
//

#import "SouSuoViewController.h"
#import "UIColor+AddColor.h"
#import "SouSuoDetailViewController.h"

@interface SouSuoViewController ()<UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, retain) UITableView *tabelView;
@property (nonatomic, retain)  NSMutableArray *sousuoArr;
@property (nonatomic, assign) NSInteger tag;  // 判断是不是第一次搜索
@end

@implementation SouSuoViewController
- (void)dealloc
{
    [_searchBar release];
    [_tabelView release];
    [_sousuoArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
   self.sousuoArr = [NSMutableArray array];
    
  
   
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 45)];
    [self.view addSubview:self.searchBar];
    self.searchBar.showsBookmarkButton = YES;
    self.searchBar.backgroundColor = [UIColor colorFromHexCode:@"#94d6da"];
    self.searchBar.placeholder = @"search";
    self.searchBar.delegate = self;
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.keyboardType = UIKeyboardTypeNamePhonePad;
    self.searchBar.showsSearchResultsButton = YES;
    self.searchBar.showsCancelButton = YES;

    //让搜索栏成为第一响应者
    [self.searchBar becomeFirstResponder];
    // 是否进行文字纠错
    _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    // 单词是否大写
    _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
//    self.tabelView.tableHeaderView = self.searchBar;
    [_searchBar release];
    
    // 搜索tableView
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.searchBar.frame.size.height + 64, self.view.frame.size.width, self.view.frame.size.height - self.searchBar.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tabelView];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [_tabelView release];
    
//    self.tabelView = [[UITableView alloc] init];
    
    // 搜索接口
  //  http://rm.app.diaoyuweng.com/json_rm.php?action=list&ver=4&page=0&category=jiaju&category2=&pageSize=10&appCookie=channel_AppStore%20version_4.1.2%20oid_0f607264fc6318a92b9e13c65db7cd3c%20package_com.aslibra.rm.jiaju%20platform_iphone%20ver_8.1.2%20jb_0&keyword=%E5%AE%B6%E5%B1%85%E6%9C%8D%E9%A5%B0
    
    
    
}
// tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SouSuoDetailViewController *sousuo = [[SouSuoDetailViewController alloc] init];
    sousuo.sousuoStr = [self.sousuoArr objectAtIndex:indexPath.row];
    self.tag = 0;  // 第一次搜索
    sousuo.tag = self.tag;
    //跳转页面
    [self.navigationController pushViewController:sousuo animated:YES];
    [sousuo release];
}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    //显示离开按钮
    
    return YES;
}

// 取消按钮实现方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    // 返回前一页
    [self.navigationController popViewControllerAnimated:YES];
    
}


// 键盘搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    SouSuoDetailViewController *sousuo = [[SouSuoDetailViewController alloc] init];
    sousuo.sousuoStr = searchBar.text;
    // 将搜索字符放到数组中
    [self.sousuoArr addObject:searchBar.text];
    NSLog(@"搜搜数组%@", self.sousuoArr);
    [self.tabelView reloadData];
    // 设置tag值为0 非第一次搜索
    self.tag = 1;
    sousuo.tag = self.tag;
    //跳转页面
    [self.navigationController pushViewController:sousuo animated:YES];
    [sousuo release];
    NSLog(@"ok");
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    searchBar.showsCancelButton = YES;
//    self.searchBar = searchText;
}

// tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"+++++++++%ld", self.sousuoArr.count);
    return self.sousuoArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str] autorelease];
        
    }
     cell.textLabel.text = [self.sousuoArr objectAtIndex:indexPath.row];
//    cell.textLabel.backgroundColor = [UIColor redColor];
    NSLog(@"文本内容%@", cell.textLabel.text);
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
